<?php

include $_SERVER['DOCUMENT_ROOT'] . '/view/include/index.php';

if (isset($_GET['delete'])) {
    $personDAO->delete($_GET['delete']);
    header("location:/view/person/");
}

if (isset($_GET['update'])) {
    $person = $personDAO->loadById($_GET['update']);
    $date = $person->getBirthday();
    $date = explode("-", $date);
    $smarty->assign("person", $person);
    $smarty->assign("b_year", $date[0]);
    $smarty->assign("b_month", $date[1]);
    $smarty->assign("b_day", $date[2]);

    $date = $person->getContratStartDate();
    $date = explode("-", $date);
    $smarty->assign("cs_year", $date[0]);
    $smarty->assign("cs_month", $date[1]);
    $smarty->assign("cs_day", $date[2]);

    $date = $person->getContratFinishDate();
    $date = explode("-", $date);
    $smarty->assign("cf_year", $date[0]);
    $smarty->assign("cf_month", $date[1]);
    $smarty->assign("cf_day", $date[2]);

    $smarty->display("person/update.tpl");
}

if (isset($_GET['new'])) {
    $smarty->display("person/new.tpl");
}

if (isset($_POST['proc'])) {
    if ($_POST['proc'] == "upsert") {
        empty($_POST['id']) ? $id = null : $id = $_POST['id'];
        $person = array(
            'id' => $id,
            'name' => $_POST['name'],
            'birthday' => $_POST['B_Date_Year'] . "-" . $_POST['B_Date_Month'] . "-" . $_POST['B_Date_Day'],
            'nationality' => array('id' => $_POST['nationality']),
            'team' => array('id' => $_POST['team']),
            'type' => $_POST['type'],
            'position' => $_POST['position'],
            'contratStartDate' => $_POST['CS_Date_Year'] . "-" . $_POST['CS_Date_Month'] . "-" . $_POST['CS_Date_Day'],
            'contratFinishDate' => $_POST['CF_Date_Year'] . "-" . $_POST['CF_Date_Month'] . "-" . $_POST['CF_Date_Day'],
        );

        $personDAO->upsert($person);


        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/JPEG")
                || ($_FILES["file"]["type"] == "image/pjpeg"))
        ) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            } else {
                echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                echo "Type: " . $_FILES["file"]["type"] . "<br />";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

                if (file_exists("upload/" . $_FILES["file"]["name"])) {
                    echo $_FILES["file"]["name"] . " already exists. ";
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "../../../images/player/" . $_POST['team'] . "_" . $_POST['name'] . ".jpg");
                    echo "Stored in: " . "upload/" . $_POST['team'] . "_" . $_POST['name'] . ".jpg";
                }
            }
        } else {
            echo "Invalid file";
        }
    }
    header("location:/view/person/");
}


$smarty->assign("persons", $personDAO->loadAll());
$smarty->display("person/index.tpl");
?>
